import time

import pyautogui
import pygetwindow as gw

from my_package.xqol_utils import find_btn_location_window, resource_path

# 帮派战斗结束场景
end_battle_img_path = resource_path("images\\gang_fight\\gang_fight_end.png")
# 自动按钮
auto_btn_img_path = resource_path("images\\gang_fight\\auto_btn.png")
# 确认按钮
confirm_btn_img_path = resource_path("images\\gang_fight\\confirm_btn.png")

def gang_fight_pk(window=None):
    """
    Given two lists of gang members, return a list of gang members who are in both gangs.
    """
    if window is None:
        # 获取当前所有窗口
        result = gw.getWindowsWithTitle("xqApp")
    else:
        result = [window]
    for window in result:
        # 激活窗口
        try:
            # 将窗口激活并置顶
            window.activate()
        except Exception as e:
            print("窗口操作失败：", e)
        # 找帮派战斗结束后的场景
        time.sleep(0.2)
        res_end = find_btn_location_window(window, end_battle_img_path)
        print("结果", res_end)
        if len(res_end) == 0:
            # 去找图
            #time.sleep(0.3)
            res_auto = find_btn_location_window(window, auto_btn_img_path)
            if res_auto:
                x_auto, y_auto = res_auto
                pyautogui.click(x_auto + window.left + 3, y_auto + window.top + 3)
        else:
            print(window.title, " 当前处于帮派战斗结束场景")
            # 找战斗结束后的确认按钮
            time.sleep(0.2)
            res_confirm = find_btn_location_window(window, confirm_btn_img_path)
            if res_confirm:
                x_confirm, y_confirm = res_confirm
                pyautogui.click(x_confirm + window.left + 10, y_confirm + window.top + 5)
                return True
            return True

if __name__ == '__main__':
    while True:
        gang_fight_pk()